import Order from '../../model/order'
import { getEventParam } from '../../utils/utils'
import roleType from '../../enum/role-type'
const order = new Order()
// pages/my-order/my-order.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['全部订单', '待处理', '待支付', '待确认', '待评价'],
    active: 0,
    role: null,
    status: null,
    roleType: roleType,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //status:-1:全部 0：待同意 1：待支付 2：待确认 3：待评价
    const role = parseInt(options.role)
    const status = parseInt(options.status)
    this.setData({
      active: status + 1,
      role,
    })
    this.data.status = status < 0 ? '' : status
    this.data.role = role
  },
  onShow() {
    this._getOrderList()
  },
  async _getOrderList() {
    const orderList = await order
      .reset()
      .getMyOrderList(this.data.role, this.data.status)
    this.setData({
      orderList,
    })
  },
  async onPullDownRefresh() {
    await this._getOrderList()
    wx.stopPullDownRefresh()
  },
  async onReachBottom() {
    if (!order.hasMoreData) {
      return
    }
    const orderList = await order.getMyOrderList(
      this.data.role,
      this.data.status
    )
    this.setData({
      orderList,
    })
  },
  async handleTabChange(event) {
    const index = getEventParam(event, 'index')
    this.data.status = index < 1 ? '' : index - 1
    await this._getOrderList()
  },
  handleNavDetail(event) {
    const order = getEventParam(event, 'order')
    wx.navigateTo({
      url: `/pages/order-detail/order-detail?role=${
        this.data.role
      }&order=${JSON.stringify(order)}`,
    })
  },
  handleChat(event) {
    const { order } = event.detail
    const targetUserId =
      order[this.data.role === roleType.PUBLISHER ? 'consumer' : 'publisher'].id

    wx.navigateTo({
      url: `/pages/conversation/conversation?targetUserId=${targetUserId}&service=${JSON.stringify(
        order.service_snap
      )}`,
    })
  },
  handleRefund(event) {
    const order = event.detail.order
    wx.navigateTo({
      url: `/pages/refund/refund?order=${JSON.stringify(order)}`,
    })
  },
})
